package com.geeksaga.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONStringer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.geeksaga.common.util.DateConvertor;
import com.geeksaga.domain.BbsEnv;
import com.geeksaga.domain.BbsRate;
import com.geeksaga.domain.User;
import com.geeksaga.service.AuthorityService;
import com.geeksaga.service.BbsEnvService;
import com.geeksaga.service.BbsRateService;

/**
 * @author newbie
 * @since 0.1
 */
@Controller
public class RateController {
	protected static final Log logger = LogFactory.getLog(RateController.class);

	public static final String RATE_UP = "up";
    public static final String RATE_DOWN = "down";
    
    @Autowired BbsRateService bbsRateService = null;
    @Autowired BbsEnvService bbsEnvService = null;
    
    @RequestMapping(value = { "/rate/rateUp.do", "/rate/rateDown.do" })
    public void rateUpDown(@RequestParam String bbsSid, @RequestParam(required = false) String mode,
            HttpServletRequest request, HttpServletResponse response) {
        
        PrintWriter out = null;
        try {
            out = response.getWriter();
            
            BbsRate rate = bbsRateService.addAfterCalculate(makeRate(bbsSid, mode, request));
            
            out.print(rateToJson(rate));
        } catch (IOException e) {
            logger.info(e.getMessage(), e);
        }
    }
    
    private BbsRate makeRate(String bbsSid, String mode, HttpServletRequest request) {
        BbsRate rate = new BbsRate();
        rate.setBbsSid(bbsSid);
        rate.setRegistTimestamp(DateConvertor.getDateTimeFormat());
        rate.setUserIp(request.getRemoteAddr());
        
        User user = AuthorityService.getUser(request.getSession());
        if(user != null)
            rate.setUserSid(user.getSid());
        
        BbsEnv env = bbsEnvService.find();
        if(env != null && RATE_UP.equals(mode))
            rate.setPoint(env.getRatePoint());
        else
            rate.setPoint(String.valueOf(Integer.valueOf(env.getRatePoint()) * (-1)));
        
        return rate;
    }
    
    private String rateToJson(BbsRate rate) {
        JSONStringer json = new JSONStringer();
        
        try {
            json.object().key("rate").array();
            json.object().key("goodCnt").value(rate.getCount()).endObject();
            json.object().key("goodSum").value(rate.getSum()).endObject();
            json.endArray().endObject();
        } catch (JSONException e) {
            logger.info(e.getMessage(), e);
        }

        return json.toString();
    }
}